package com.fumasoft.dynamic.utils;

import com.alibaba.fastjson.JSON;
import com.fumasoft.bill.server.enums.ResultCode;
import com.fumasoft.bill.server.exceptions.BizException;
import com.fumasoft.common.dto.TokenInfoDTO;
import com.fumasoft.dynamic.contant.Constants;
import com.fumasoft.dynamic.ttl.CustomerTTL;
import com.fumasoft.dynamic.vo.TokenInfoVo;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * token工具类.
 */
public class TokenUtils {

    /**
     * token参数名称.
     */
    @Resource
    public static final String TOKEN_PARAMETER = "tokenInfo";

    /**
     * 语言参数名称.
     */
    public static final String LANGUAGE_TYPE = "Language-Type";

    /**
     * 默认token.
     */
    public static final String DEFAULT_TOKEN = "{\"cId\":16085,\"ownerId\":260819264,\"value\":\"WVGZ4vDsHKoMFDCN\"}";

    /**
     * 验证token.
     *
     * @param request 请求
     * @return 用户id
     */
    public static Long validate(HttpServletRequest request) {
        return null;
    }

    /**
     * 获取用户名.
     *
     * @return 用户名
     */
    public static String getUserName() {
        return "admin";
    }

    /**
     * 获取用户token.
     *
     * @return 用户token
     */
    public static String getToken() {
        return CustomerTTL.tokenInfo.get().getValue();
    }

    /**
     * 获取用户token.
     *
     * @return 用户token
     */
    public static String getToken(ServletRequest request) {
        String tokenInfo = ((HttpServletRequest) request).getHeader(TokenUtils.TOKEN_PARAMETER);
        if (StringUtils.isEmpty(tokenInfo)) {
            return "";
        }
        TokenInfoVo tokenInfoVo = JSON.parseObject(tokenInfo, TokenInfoVo.class);
        String tokenFrequency = Constants.TOKEN_FREQUENCY + tokenInfoVo.getToken();
        return tokenFrequency;
    }

    /**
     * 获取用户token.
     *
     * @return 用户token
     */
    public static TokenInfoVo getTokenInfoVo() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw new BizException(ResultCode.SYSTEM_ERROR);
        }
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String tokenInfo = request.getHeader(TOKEN_PARAMETER);
        if (StringUtils.isEmpty(tokenInfo)) {
            throw new BizException(ResultCode.TOKEN_NOT_FOUND_ERROR);
        }
        TokenInfoVo tokenInfoVo = JSON.parseObject(tokenInfo, TokenInfoVo.class);
        if (Objects.isNull(tokenInfoVo) || StringUtils.isEmpty(tokenInfoVo.getToken())) {
            throw new BizException(ResultCode.TOKEN_NOT_FOUND_ERROR);
        }
        return tokenInfoVo;
    }

    /**
     * 获取用户token.
     *
     * @return 用户token
     */
    public static String getFilterToken(ServletRequest request) {
        String tokenInfo = ((HttpServletRequest) request).getHeader(TokenUtils.TOKEN_PARAMETER);
        if (StringUtils.isEmpty(tokenInfo)) {
            return "";
        }
        TokenInfoVo tokenInfoVo = JSON.parseObject(tokenInfo, TokenInfoVo.class);
        return tokenInfoVo.getToken();
    }

    /**
     * 获取用户token和url.
     *
     * @return 用户token
     */
    public static String getTokenAndUrl(ServletRequest request) {
        String token = getToken(request);
        String requestURI = ((HttpServletRequest) request).getRequestURI();
        return token + requestURI;
    }

    /**
     * 获取用户语言类型.
     *
     * @return 用户语言类型
     */
    public static String getLanguageType() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            throw new BizException(ResultCode.SYSTEM_ERROR);
        }
        return getLanguageType(requestAttributes);
    }

    /**
     * 中文（zh-cn）、2：繁体(zh-tw)、3：英文(en).
     *
     * @return 语言类型
     */
    public static String getLanguageType(RequestAttributes requestAttributes) {
        String defaultType = "zh-cn";
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String languageType = request.getHeader(LANGUAGE_TYPE);
        if (StringUtils.isEmpty(languageType)) {
            return defaultType;
        }
        return languageType;
    }

    /**
     * 获取企业标签.
     *
     * @return 企业标签
     */
    public static String getCompanyTag() {
        TokenInfoDTO tokenInfoDTO = CustomerTTL.tokenInfo.get();
        if (tokenInfoDTO == null) {
            return null;
        }
        return tokenInfoDTO.getCompanyTag();
    }
}
